/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package vn.com.pps.web.common.component.table;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import javax.faces.model.ListDataModel;
/**
 *
 * @author Dai
 */
public abstract class AbstractModel
        implements Serializable {
    protected static final int MAXIMUM_LARGE_PAGE_SIZE = 500;
    protected Class m_itemClass;
    protected ArrayList m_pageItems;
    protected AbstractTableItem m_selectedItem;
    protected transient ListDataModel m_listModel;
    protected int m_pageCount;
    protected int m_selectedPage = 1;
    protected int m_rowCount;
    protected int m_targetPage = 1;
    protected int m_initialMaximumPageSize=10;
    protected int m_maximumPageSize=10;
    

    public AbstractModel() {
        this.m_pageItems = new ArrayList();
        setMaximumPageSize(10);
    }

    public AbstractModel(Class pm_itemClass) {
        this.m_itemClass = pm_itemClass;
    }

    public void setPageItems(ArrayList m_pageItems) {
        this.m_pageItems = m_pageItems;
    }

    public ArrayList getPageItems()
    {
        return m_pageItems;
    }

    public AbstractTableItem createItem() {
        try {
            return (AbstractTableItem) this.m_itemClass.newInstance();
        } catch (Exception e) {
        }

        return null;
    }

    public void setMaximumPageSize(int pm_size) {
        this.m_initialMaximumPageSize = (this.m_maximumPageSize = pm_size);
    }

    public int getMaximumPageSize()
    {
        return m_maximumPageSize;
    }
    public ListDataModel getPageData() {
        if (this.m_listModel == null) {
            this.m_listModel = new ListDataModel();
        }
       
        this.m_listModel.setWrappedData(this.m_pageItems);
        return this.m_listModel;
    }

    public int getPageItemCount() {
        return this.m_pageItems.size();
    }

    protected void newPage() {
        this.m_pageItems = new ArrayList();
    }

    public String newItem() {
        AbstractTableItem item = createItem();
        item.setIsNew(true);

        this.m_selectedItem = item;
        return "success";
    }

    public void addItem(AbstractTableItem pm_item) {
        this.m_pageItems.add(pm_item);
    }

    public void removeItem(AbstractTableItem pm_item) {
        if (this.m_pageItems != null) {
            this.m_pageItems.remove(pm_item);
        }
    }

    private void setSelectedStatus(boolean pm_status) {
        Iterator iter = this.m_pageItems.iterator();
        while (iter.hasNext()) {
            AbstractTableItem item = (AbstractTableItem) iter.next();
            item.setSelected(pm_status);
        }
    }

    public synchronized String selectAll() {
        setSelectedStatus(true);
        return "success";
    }

    public synchronized String selectNone() {
        setSelectedStatus(false);
        return "success";
    }

    public synchronized String selectInvert() {
        Iterator iter = this.m_pageItems.iterator();
        while (iter.hasNext()) {
            AbstractTableItem item = (AbstractTableItem) iter.next();
            item.setSelected(!item.getSelected());
        }

        return "success";
    }

    public int getRowCount() {
        return this.m_rowCount;
    }

    public void setRowCount(int pm_rowCount) {
        this.m_rowCount = pm_rowCount;
        this.m_pageCount = (int) Math.ceil(this.m_rowCount / this.m_maximumPageSize);
        if (m_pageCount * m_maximumPageSize != m_rowCount) {
            m_pageCount++;
        }
    }

    public String onRowSelected() {
        this.m_selectedItem = null;
        try {
            this.m_selectedItem = ((AbstractTableItem) this.m_listModel.getRowData());
        } catch (IllegalArgumentException e) {
            this.m_selectedItem = getSelectedItem();
        }

        return "success";
    }

    public AbstractTableItem getSelectedItem() {
        if (this.m_selectedItem == null) {
            Iterator iter = this.m_pageItems.iterator();
            while (iter.hasNext()) {
                AbstractTableItem item = (AbstractTableItem) iter.next();
                if (item.getSelected()) {
                    this.m_selectedItem = item;
                    break;
                }
            }
        }
        return this.m_selectedItem;
    }

    public ArrayList getSelectedItems() {
        ArrayList list = new ArrayList();

        Iterator iter = this.m_pageItems.iterator();
        while (iter.hasNext()) {
            AbstractTableItem item = (AbstractTableItem) iter.next();
            if (item.getSelected()) {
                list.add(item);
            }
        }

        return list;
    }

    public String getPageCount() {
        return String.valueOf(this.m_pageCount);
    }

    public String getSelectedPage() {
        return String.valueOf(this.m_selectedPage);
    }

    public boolean isNormalPageMode() {
        return this.m_initialMaximumPageSize == this.m_maximumPageSize;
    }

    public void switchPageMode() {
        this.m_maximumPageSize = ((isNormalPageMode()) ? 500 : this.m_initialMaximumPageSize);

        reLoad();
    }

    public void reLoad() {
    }

    public abstract boolean getHasNextPage();

    public abstract boolean getHasPreviousPage();

    public abstract void firstPage();

    public abstract void lastPage();

    public abstract void nextPage();

    public abstract void previousPage();
}
